package com.nbjtjc.safe.mapper.cost;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.model.cost.CostInjuryClaim;
import com.nbjtjc.util.MyMapper;
@Mapper
public interface CostBudgetPlanMapper extends MyMapper<CostBudgetPlan> {

	CostBudgetPlan show(@Param("id") int id);

	List<CostBudgetPlan> search(@Param("cbp") CostBudgetPlan costBudgetPlan);
	
	List<CostBudgetPlan> searchDetail(@Param("cbp") CostBudgetPlan costBudgetPlan);
	
	List<CostBudgetPlan> searchItems(@Param("cbp") CostBudgetPlan costBudgetPlan);

	List<CostBudgetPlan> searchMonth(@Param("cbp") CostBudgetPlan costBudgetPlan);
	
	
	List<CostBudgetPlan> searchByYear(@Param("year") Date y,@Param("deptId") int companyId);
	
	List<CostBudgetPlan> searchDetailByYear(@Param("year") Date y,@Param("deptId") int companyId);

	@Insert(
			"INSERT INTO  cost_budget_plan (title,`month`,parent_id,dept_id) "
			+ "(SELECT CONCAT(DATE_FORMAT(#{cbp.year},'%Y-'),`month_num`,'月-预算计划') AS title,month_num AS `month`,#{cbp.id} AS parent_id,#{cbp.deptId} dept_id FROM cost_month)"
			)
	int createMonthPlan(@Param("cbp") CostBudgetPlan costBudgetPlan);

	List<CostBudgetPlan> searchSubcompany(@Param("deptId") int deptId,@Param("year") Date year);
	
	List<CostBudgetPlan> searchSubcompanyItems(@Param("deptId") int deptId,@Param("year") Date year);


	@Update("UPDATE cost_budget_plan SET total_budget = (SELECT SUM(item_total_budget) FROM cost_budget_item WHERE budget_plan_id = #{planId} AND cancel = 0 ) WHERE id = #{planId} ")
	int updateTotalBudget(@Param("planId") Integer id);

	List<CostBudgetPlan> searchTrackSubcompany(@Param("deptId") int deptId,@Param("year") Date year);


	
//	@Select("select * from cost_budget_plan where cancel = 0 and parent_id = #{id}")
//	List<CostBudgetPlan> findMonthPlanByParentId(@Param("id")Integer id);

}